package com.woniuxy.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.woniuxy.common.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.SpringfoxWebMvcConfiguration;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: Hello Mr. Guo
 * @Date:2021/5/18 14 18
 * @Description: Swagger2API文档的配置
 */

@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config {
    @Bean
    public Docket createRestApi(){

        List list = new ArrayList();
        Arrays.stream(ResultEnum.values()).forEach(resultEnum -> {
            list.add(new ResponseMessageBuilder().code(resultEnum.getCode()).message(resultEnum.getMessage()).build());
        });

        return new Docket(DocumentationType.SWAGGER_2)
                .globalResponseMessage(RequestMethod.GET,list)
                .globalResponseMessage(RequestMethod.POST,list)
                .globalResponseMessage(RequestMethod.PUT,list)
                .globalResponseMessage(RequestMethod.DELETE,list)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                //.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //.apis(RequestHandlerSelectors.basePackage("com.woniuxy.emp.controller"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())//监控所有请求路径
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("足浴管理系统")
                .description("足浴管理系统")
                .contact(new Contact("admin","",""))
                .version("1.0")
                .build();
    }
}
